package com.mongo.demo.service.impl;

import com.mongo.demo.model.InvoiceRiskDocument;
import com.mongo.demo.model.ThirdPartyInvoiceInfo;
import com.mongo.demo.service.InvoiceRiskDocumentGenerationService;
import com.mongo.demo.service.ThirdPartyInvoiceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class InvoiceRiskDocumentGenerationServiceImpl implements InvoiceRiskDocumentGenerationService {

    @Autowired
    private ThirdPartyInvoiceInfoService thirdPartyInvoiceInfoService;

    @Override
    public InvoiceRiskDocument generationDocument(String entName, String entCreditNo) {

        InvoiceRiskDocument invoiceRiskDocument = InvoiceRiskDocument.builder()
                .entName(entName)
                .entCreditCode(entCreditNo)
                .build();

        List<ThirdPartyInvoiceInfo> invoiceInfos = getInvoiceInfos(entName);
        if (CollectionUtils.isEmpty(invoiceInfos)) {
            return invoiceRiskDocument;
        }
        invoiceRiskDocument.setInvoiceInfos(invoiceInfos);

        Map<String, Object> invoiceIndicators = calculationInvoiceIndicators(invoiceInfos);
        invoiceRiskDocument.setInvoiceIndicators(invoiceIndicators);


        return invoiceRiskDocument;
    }

    protected Map<String, Object> calculationInvoiceIndicators(List<ThirdPartyInvoiceInfo> invoiceInfos) {
        LinkedHashMap<String, Object> indicators = new LinkedHashMap<>();
        indicators.put("近12个月超过去年收入均值的90%的次数", invoiceInfos.size());
        indicators.put("近3个月总销项发票", invoiceInfos.stream().map(ThirdPartyInvoiceInfo::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        indicators.put("近12个月收入同比增幅", invoiceInfos.stream().map(ThirdPartyInvoiceInfo::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.valueOf(invoiceInfos.size()), 2, RoundingMode.HALF_UP));
        return indicators;
    }

    protected List<ThirdPartyInvoiceInfo> getInvoiceInfos(String entName) {
        return thirdPartyInvoiceInfoService.lambdaQuery().eq(ThirdPartyInvoiceInfo::getBuyerName, entName).list();
    }

    protected List<ThirdPartyInvoiceInfo> getAllInvoiceInfos() {
        return thirdPartyInvoiceInfoService.list();
    }

    public List<InvoiceRiskDocument> generationDocuments() {
        List<ThirdPartyInvoiceInfo> invoiceInfos = getAllInvoiceInfos();
        if (CollectionUtils.isEmpty(invoiceInfos)) {
            return new ArrayList<>();
        }
        List<InvoiceRiskDocument> result = new ArrayList<>();
        Map<String, List<ThirdPartyInvoiceInfo>> map = invoiceInfos.stream().collect(Collectors.groupingBy(ThirdPartyInvoiceInfo::getBuyerName));
        for (Map.Entry<String,List<ThirdPartyInvoiceInfo>> entry: map.entrySet()) {
            result.add(InvoiceRiskDocument.builder()
                    .entName(entry.getKey())
                    .entCreditCode("4321")
                    .invoiceInfos(entry.getValue())
                    .invoiceIndicators(calculationInvoiceIndicators(entry.getValue()))
                    .build());
        }
        return result;
    }
}
